package com.situ.crm2026.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm2026.model.Coach;
import com.situ.crm2026.model.dto.CoachDto;
import com.situ.crm2026.service.CoachService;
import com.situ.crm2026.service.UploadService;
import com.situ.crm2026.util.JsonResult;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.Media;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/coaches", produces = MediaType.APPLICATION_JSON_VALUE)
public class CoachApi {

    private CoachService coachService;

    @Autowired
    public void setCoachService(CoachService coachService) {
        this.coachService = coachService;
    }


    //依赖注入上传头像的实例
    private UploadService uploadService;

    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    //查询宣布教练以及分页
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Coach csb) {
        Page<Coach> page = new Page<>(pageNo, pageSize);
        page = this.coachService.findAll(page, csb);
        return ResponseEntity.ok(JsonResult.success(page));
    }


    //上传教练头像
    @PostMapping("/avatar")
    public ResponseEntity<JsonResult<?>> uploadCoachAvatar(MultipartFile file) {
        String url = this.uploadService.uploadImage(file, "coach");
        return ResponseEntity.ok(JsonResult.success(url));
    }

    //批量删除教练

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(
            @RequestBody
            @Validated
            @Size(min = 1, message = "要删除的教练ID至少1个") Integer[] ids) {
        int count = this.coachService.deleteByIds(List.of(ids));
        return ResponseEntity.ok(JsonResult.success(count));
    }

    //添加教练
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody @Validated Coach coach){
        boolean success=this.coachService.save(coach);
        if(success){
            return ResponseEntity.ok(JsonResult.success(coach));
        }else{
            return ResponseEntity.ok(JsonResult.fail("保存失败"));
        }
    }
    //修改教练
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody @Validated Coach coach){
        boolean success=this.coachService.update(coach);
        if(success){
            return ResponseEntity.ok(JsonResult.success(coach));
        }else{
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }


    // 根据教练ID获取团课学生信息
    @GetMapping("/{id}/course-students")
    public ResponseEntity<JsonResult<?>> getCourseStudentsByCoachId(@PathVariable Integer id) {
        try {
            List<CoachDto> students = this.coachService.findByCoachId(id);
            return ResponseEntity.ok(JsonResult.success(students));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(JsonResult.fail("查询团课学生信息失败: " + e.getMessage()));
        }
    }


    // 根据教练ID获取私课学生信息
    @GetMapping("/{id}/private-students")
    public ResponseEntity<JsonResult<?>> getPrivateStudentsByCoachId(@PathVariable Integer id) {
        try {
            List<CoachDto> students = this.coachService.findByPrivateId(id);
            return ResponseEntity.ok(JsonResult.success(students));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(JsonResult.fail("查询团课学生信息失败: " + e.getMessage()));
        }
    }
}



